package com.company.beauty.infra.spi.ids;

import com.company.beauty.infra.spi.core.ServiceLoader;
import com.google.common.base.Strings;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;
import java.util.Properties;

/**
 * <p>snowflake handler factory</p >
 *
 * @author youyoukele
 * @version 1.0
 * @date 2024/2/2 14:02
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SnowflakeHandlerFactory {

    public static final String DEFAULT_SNOWFLAKE_HANDLER = "DEFAULT";

    static {
        ServiceLoader.registerTypedService(SnowflakeHandler.class);
    }

    /**
     * Get snowflake handler.
     *
     * @param type  snowflake handler type
     * @param props snowflake handler properties
     * @return snowflake handler
     */
    public static Optional<SnowflakeHandler> createHandler(final String type, final Properties props) {
        log.info("createHandler type=[{}], props=[{}]", type, props);
        if (Strings.isNullOrEmpty(type)) {
            return ServiceLoader.newTypedServiceInstance(SnowflakeHandler.class, DEFAULT_SNOWFLAKE_HANDLER, props);
        }
        return ServiceLoader.newTypedServiceInstance(SnowflakeHandler.class, type, props);
    }

}
